//
//  UIColor+TYTheme.m
//  TYOEMConfig
//
//  Created by 盲僧 on 2020/7/8.
//

#import "UIColor+TYTheme.h"
#import "objc/runtime.h"
#import "TYTheme.h"
@interface UIColor ()
@property (nonatomic,assign) NSNumber *ty_isDarkcolor;

@end

@implementation UIColor (TYTheme)

- (UIColor *)N1
{
    if ([[self ty_isDarkcolor] boolValue]) {
        if ([self ty_isBtnThemeColor]) {
            return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        }
        return [UIColor colorWithRed:1 green:1 blue:1 alpha:TYTheme.theme.N1];
    } else {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N1];
    }
    return nil;
}

- (UIColor *)N2
{
    if ([[self ty_isDarkcolor] boolValue]) {
        if ([self ty_isBtnThemeColor]) {
            return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        }
        return [UIColor colorWithRed:1 green:1 blue:1 alpha:TYTheme.theme.N2];
    } else {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N2];
    }
    return nil;
}

- (UIColor *)N3
{
    if ([[self ty_isDarkcolor] boolValue]) {
        if ([self ty_isBtnThemeColor]) {
            return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        }
        return [UIColor colorWithRed:1 green:1 blue:1 alpha:TYTheme.theme.N3];
    } else {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N3];
    }
    return nil;
}

- (UIColor *)N4
{
    if ([[self ty_isDarkcolor] boolValue]) {
        if ([self ty_isBtnThemeColor]) {
            return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        }
        return [UIColor colorWithRed:1 green:1 blue:1 alpha:TYTheme.theme.N4];
    } else {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N4];
    }
    return nil;
}

- (UIColor *)N5
{
    if ([[self ty_isDarkcolor] boolValue]) {
        if ([self ty_isBtnThemeColor]) {
            return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        }
        return [UIColor colorWithRed:1 green:1 blue:1 alpha:TYTheme.theme.N5];
    } else {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N5];
    }
    return nil;
}

- (UIColor *)N6
{
    if ([[self ty_isDarkcolor] boolValue]) {
        if ([self ty_isBtnThemeColor]) {
            return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        }
        return [UIColor colorWithRed:1 green:1 blue:1 alpha:TYTheme.theme.N6];
    } else {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N6];
    }
    return nil;
}

- (UIColor *)N7
{
    if ([[self ty_isDarkcolor] boolValue]) {
        if ([self ty_isBtnThemeColor]) {
            return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        }
        return [UIColor colorWithRed:1 green:1 blue:1 alpha:TYTheme.theme.N7];
    } else {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N7];
    }
    return nil;
}

- (UIColor *)N8
{
    if ([[self ty_isDarkcolor] boolValue]) {
        if ([self ty_isBtnThemeColor]) {
            return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        }
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N8];
    } else {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:TYTheme.theme.N8];
    }
    return nil;
}

- (UIColor *)ty_highlyColor
{
    UIColor *forwordColor;
    if ([[self ty_isDarkcolor] boolValue]) {
        forwordColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
    } else {
        forwordColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    }
    return [self ty_colorWithForwordColor:forwordColor];
}

- (UIColor *)ty_colorWithForwordColor:(UIColor *)forwordColor {
    CGFloat red1 = [forwordColor ty_ThemeRed];
    CGFloat green1 = [forwordColor ty_ThemeGreen];
    CGFloat blue1 = [forwordColor ty_ThemeBlue];
    CGFloat alpha1 = [forwordColor ty_ThemeAlpha];
    
    CGFloat red2 = [self ty_ThemeRed];
    CGFloat green2 = [self ty_ThemeGreen];
    CGFloat blue2 = [self ty_ThemeBlue];
    CGFloat alpha2 = [self ty_ThemeAlpha];
    
    CGFloat alpha = 1-(1-alpha1)*(1-alpha2);
    CGFloat red = (alpha1*red1+(1-alpha1)*red2*alpha2)/alpha;
    CGFloat green = (alpha1*green1+(1-alpha1)*green2*alpha2)/alpha;
    CGFloat blue = (alpha1*blue1+(1-alpha1)*blue2*alpha2)/alpha;
    
    UIColor *rColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    return rColor;
}


- (NSNumber *)ty_isDarkcolor {
    NSNumber *isDark = objc_getAssociatedObject(self, _cmd);
    if (!isDark) {
        CGFloat hue;
        CGFloat saturation;
        CGFloat brightness;
        CGFloat alpha;
        [self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
        if (saturation >= 0 && saturation <= 0.4 && brightness >= 0.6 && brightness<= 1) {
            isDark = @(NO);
        } else {
            isDark = @(YES);
        }
        [self setTy_isDarkcolor:isDark];
    }
    return isDark;
}

- (void)setTy_isDarkcolor:(NSNumber *)ty_isDarkcolor {
    objc_setAssociatedObject(self, @selector(ty_isDarkcolor), ty_isDarkcolor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGFloat)ty_ThemeRed {
    CGFloat r = 0, g, b, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return r;
}

- (CGFloat)ty_ThemeGreen {
    CGFloat r, g = 0, b, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return g;
}

- (CGFloat)ty_ThemeBlue {
    CGFloat r, g, b = 0, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return b;
}

- (CGFloat)ty_ThemeAlpha {
    return CGColorGetAlpha(self.CGColor);
}

- (UIColor *)ty_DisableColor
{
    CGFloat red = [self ty_ThemeRed];
    CGFloat green = [self ty_ThemeGreen];
    CGFloat blue = [self ty_ThemeBlue];
    return [UIColor colorWithRed:red green:green blue:blue alpha:0.2];
}

+ (CGFloat)interpolationFrom:(CGFloat)from to:(CGFloat)to percent:(CGFloat)percent
{
    percent = MAX(0, MIN(1, percent));
    return from + (to - from)*percent;
}

+ (UIColor *)interpolationColorFrom:(UIColor *)fromColor to:(UIColor *)toColor percent:(CGFloat)percent
{
    CGFloat red = [self interpolationFrom:fromColor.ty_ThemeRed to:toColor.ty_ThemeRed percent:percent];
    CGFloat green = [self interpolationFrom:fromColor.ty_ThemeGreen to:toColor.ty_ThemeGreen percent:percent];
    CGFloat blue = [self interpolationFrom:fromColor.ty_ThemeBlue to:toColor.ty_ThemeBlue percent:percent];
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

+ (UIColor *)ty_themeColorWithHex:(uint32_t)hex {
    CGFloat preAlpha = ((float)((hex & 0xFF000000) >> 24))/255.0f;
    CGFloat alpha = preAlpha > 0 ? preAlpha : 1;
    return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0
                           green:((float)((hex & 0xFF00) >> 8))/255.0
                            blue:((float)(hex & 0xFF))/255.0 alpha:alpha];
}

+ (UIColor *)ty_themeColorWithHexString:(NSString *)hexColor {
    if (![hexColor isKindOfClass:[NSString class]]) {
        return nil;
    }
    NSString *colorStr = [[hexColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([colorStr length] < 6 || [colorStr length] > 9) {
        return nil;
    }
    if ([colorStr hasPrefix:@"#"]) {
        colorStr = [colorStr substringFromIndex:1];
    }
    
    unsigned int hex;
    [[NSScanner scannerWithString:colorStr] scanHexInt:&hex];
    return [self ty_themeColorWithHex:hex];
}

+ (UIColor *)ty_themeColorWithHex:(uint32_t)hex alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0
                           green:((float)((hex & 0xFF00) >> 8))/255.0
                            blue:((float)(hex & 0xFF))/255.0 alpha:alpha];
}

- (UIColor *)ty_themeColorWithAlpha:(CGFloat)alpha; {
    CGFloat r, g, b;
    [self getRed:&r green:&g blue:&b alpha:nil];
    return [UIColor colorWithRed:r green:g blue:b alpha:alpha];
}

+ (UIColor *)ty_themeColorWithHexString:(NSString *)hexColor alpha:(CGFloat)alpha {
    if (![hexColor isKindOfClass:[NSString class]]) {
        return nil;
    }
    NSString *colorStr = [[hexColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([colorStr length] < 6 || [colorStr length] > 9) {
        return nil;
    }
    if ([colorStr hasPrefix:@"#"]) {
        colorStr = [colorStr substringFromIndex:1];
    }

    // Scan values
    unsigned int hex;
    [[NSScanner scannerWithString:colorStr] scanHexInt:&hex];
    return [self ty_themeColorWithHex:hex alpha:alpha];
}

- (UIImage *)ty_convertToImage {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.f, 1.f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (BOOL)ty_isBtnThemeColor
{
    if (self == TYTheme.theme.M1 ||
        self == TYTheme.theme.M2 ||
        self == TYTheme.theme.M3 ||
        self == TYTheme.theme.M4 ||
        self == TYTheme.theme.M5 ) {
        return YES;
    }
    return NO;
}

@end
